package com.serie402.business.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.kiss.fw.dto.AbstractDAODTO;
import com.kiss.fw.exceptions.DAOException;
import com.kiss.fw.utils.Constants;
import com.serie402.business.dao.dto.GalleryDAODTO;
import com.serie402.business.dao.mappers.GalleryMapper;
import com.serie402.common.bo.PhotoAlbum;

public final class GalleryDAO extends Serie402DAO {
	private static final Logger logger = Logger.getLogger(GalleryDAO.class);
	private static final GalleryDAO instance = new GalleryDAO();
	
	private GalleryDAO() {
	}
	
	private static GalleryDAODTO getGalleryDAODTO(final AbstractDAODTO _dto) {
		return (GalleryDAODTO)_dto;
	}
	
	public static GalleryDAO getInstance() {
		return instance;
	}
	
	@Override
	public AbstractDAODTO create(AbstractDAODTO _dto) throws DAOException {
		return _dto;
	}
	
	@Override
	public AbstractDAODTO read(AbstractDAODTO _dto) throws DAOException {
		GalleryDAODTO dto = getGalleryDAODTO(_dto);
		SqlSession sqlSession = openSqlSession();
		
		if(sqlSession != null) {
			// will hold the album(s) that will be returned
			List<PhotoAlbum> photoAlbums = null;
			
			// getting Mapper
			GalleryMapper galleryMapper = MapperFactory.getGalleryMapper(sqlSession);
			
			if(galleryMapper != null) {
				if(dto == null) {
					// create instance
					dto = new GalleryDAODTO();
					
					try {
						photoAlbums = galleryMapper.retrievePhotoAlbums();
					}
					catch(Exception _exception) {
						handleException(logger, "GalleryMapper.retrievePhotoAlbums() - Error", _exception);
					}
					finally {
						// close sqlSessing
						closeSqlSession(sqlSession);
					}
					
					if(photoAlbums != null && photoAlbums.size() > 0) {
						if(logger.isDebugEnabled()) {
							logger.debug("photoAlbums.size() = " +photoAlbums.size());
						}
						
						for(int i=0; i < photoAlbums.size(); i++) {
							PhotoAlbum tempAlbum = photoAlbums.get(i);
							
							if(tempAlbum.getPhotos().size() > 0) {
								for(int j=0; j < tempAlbum.getPhotos().size(); j++) {
									if(tempAlbum.getThumbnail().getPhotoId() == tempAlbum.getPhotos().get(j).getPhotoId()) {
										tempAlbum.setThumbnail(tempAlbum.getPhotos().get(j));
										break;
									}
								}
							}
							
							if(logger.isDebugEnabled() && photoAlbums.get(i).getPhotos() != null) {
								logger.debug("photoAlbums.get(" +i +").getPhotos().size() = " +photoAlbums.get(i).getPhotos().size());
							}
						}
					}
					
					dto.setPhotoAlbums(photoAlbums);
				}
				else if(dto.getAlbumId() > 0) {
					PhotoAlbum photoAlbum = null;
					
					try {
						// retrieve the photo album
						photoAlbum = galleryMapper.retrievePhotoAlbum(dto);
					}
					catch(Exception _exception) {
						handleException(logger, "GalleryMapper.retrievePhotoAlbum(int) - Error", _exception);
					}
					finally {
						// close sqlSessing
						closeSqlSession(sqlSession);
					}
					
					// before adding to list verify that a photo album did come back
					if(photoAlbum != null && photoAlbum.getAlbumId() > 0) {
						photoAlbums = new ArrayList<PhotoAlbum>(1);
						photoAlbums.add(photoAlbum);
					}
				}
			}
			else {
				logger.error("Null Mapper returned");
				throw Constants.Exceptions.MAPPER_EXCEPTION;
			}
		}
		
		return dto;
	}
	

	@Override
	public AbstractDAODTO update(AbstractDAODTO _dto) throws DAOException {
		return _dto;
	}
	

	@Override
	public AbstractDAODTO delete(AbstractDAODTO _dto) throws DAOException {
		return _dto;
	}

	@Override
	public AbstractDAODTO search(AbstractDAODTO _dto) throws DAOException {
		return _dto;
	}
}